package com.freedom.auth.controller;

import com.freedom.auth.service.IMenuService;
import com.freedom.auth.domain.Menu;
import com.freedom.auth.query.MenuQuery;
import com.freedom.base.util.AjaxResult;
import com.freedom.base.util.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.List;

@RestController
@RequestMapping("/menu")
public class MenuController {
    @Autowired
    public IMenuService menuService;


    /**
     * 保存和修改公用的
     * @param menu 传递的实体
     * @return Ajaxresult转换结果
     */
    @PostMapping
    public AjaxResult insertOrUpdate ( @RequestBody Menu menu) {
        try {
            if ( menu.getId() != null){
                    menuService.update(menu);
                return AjaxResult.success("修改成功");
            }else{
                    menuService.insert(menu);
                return AjaxResult.success("新增成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("保存失败！" + e.getMessage());
        }
    }

    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value = "/{id}")
    public AjaxResult delete ( @PathVariable("id") Serializable id ) {
        try {
                menuService.deleteById(id);
            return AjaxResult.success("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("删除失败！" + e.getMessage());
        }
    }

    //获取用户
    @GetMapping("/{id}")
    public AjaxResult getById ( @PathVariable("id") Serializable id ) {
        try {
            Menu menu =menuService.getById(id);
            return AjaxResult.success(menu, "查询成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("查询失败！" + e.getMessage());
        }
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping
    public AjaxResult getAll () {
        try {
            List< Menu> list = menuService.getAll();
            return AjaxResult.success(list, "查询成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("查询失败！" + e.getMessage());
        }
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PutMapping
    public AjaxResult queryPage ( @RequestBody MenuQuery query ) {
        try {
            PageInfo<Menu> pageInfo = menuService.queryPage(query);
            return AjaxResult.success(pageInfo, "分页查询成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("分页查询失败！" + e.getMessage());
        }
    }


    /**
     * 批量删除
     */
    @PatchMapping
    public AjaxResult batchDelete ( @RequestBody List<Long> ids ) {
        try {
                menuService.batchDelete(ids);
            return AjaxResult.success("批量删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("批量删除失败");
        }
    }


}
